<?php
/*
 * Created on 2009-3-25
 *
 * Class for db operations
 * created by Joe Qiao [mynamesucks@163.com]
 * You can copy this file to wherever you want, but you must remain the author info
 */

class db {
	private $connect;
	
	public function __construct() {
		//database connection
		$this->connect = mysql_connect(DATABASE_HOST, DATABASE_USERNAME, DATABASE_PASSWORD) or trigger_error(mysql_error(),E_USER_ERROR);
		mysql_select_db(DATABASE_NAME);
		mysql_query('SET NAMES utf8');
	}
	
	public function query($sql) {
		//echo $sql.'<br>';
		if($res = mysql_query($sql)) {
			return $res;
		}
		else {
			echo mysql_errno($this->connect) . ": " . mysql_error($this->connect) . "\n";
			die('There is some errors when query, please check.');
		}
	}
	
	public function num($res) {
		$num = mysql_num_rows($res);
		return $num;
	}
	
	public function insert_id() {
		$insert_id = mysql_insert_id();
		return $insert_id;
	}
	
	public function fetch($res) {
		$row = mysql_fetch_array($res);
		return $row;
	}
}
?>